<?php

namespace App\Admin\Controllers;

use App\Models\Post;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class PostController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header(trans('admin.index'))
            ->description(trans('admin.description'))
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header(trans('admin.detail'))
            ->description(trans('admin.description'))
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header(trans('admin.edit'))
            ->description(trans('admin.description'))
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header(trans('admin.create'))
            ->description(trans('admin.description'))
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Post);

        $grid->id('ID');
        $grid->category()->name('分类');
        $grid->user()->name('用户');
        $grid->body('内容')->limit(30)->filter()->modal('全部内容', function ($model) {
            return $model->body;
        });
        // 显示多图
        $grid->column('images')->display(function ($images) {  return $images; })->image('', 100, 100);
        $grid->liked('点赞')->editable()->sortable();
        $grid->commented('评论')->editable()->sortable()->help('设为-1则不显示评论');
        $grid->check_id('检查')->editable()->sortable()->help('发布人未审核ID，审核后改为1就行了');
        $grid->video('视频')->editable()->filter()->hide();
        $grid->down_point('积分')->editable()->sortable()->help('积分单价，如果有密令值，下载该密令的成本')->hide();
        $grid->free_reward('激励')->editable()->sortable()->help('允许用户激励后免费获取密令')->hide();
        $grid->created_at(trans('admin.created_at'))->hide();
        $grid->updated_at(trans('admin.updated_at'))->hide();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Post::findOrFail($id));

        $show->id('ID');
        $show->user_id('user_id');
        $show->check_id('check_id');
        $show->rank('rank');
        $show->liked('liked');
        $show->body('body');
        $show->images('images');
        $show->video('video');
        $show->category_id('category_id');
        $show->created_at(trans('admin.created_at'));
        $show->updated_at(trans('admin.updated_at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Post);

        $form->display('id','ID');
        $form->text('category_id', 'category_id');
        $form->text('user_id', 'user_id');
        $form->text('check_id', 'check_id');
        $form->text('rank', 'rank');
        $form->text('liked', 'liked');
        $form->textarea('body', 'body');
        $form->multipleImage('images', '图片')->uniqueName()->removable()->sortable();
        $form->text('video', '视频');
        $form->display('created_at',trans('admin.created_at'));
        $form->display('updated_at',trans('admin.updated_at'));


        return $form;
    }
}
